package com.aboo.web.common.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.aboo.common.exception.CustomException;

public class CustomExceptionHandler implements HandlerExceptionResolver {
	
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		if (ex instanceof CustomException) {
			map.put("msg", ex.getMessage());
		} else if (ex instanceof MaxUploadSizeExceededException) {
			map.put("msg", "文件内容过大！");
		} else {
			ex.printStackTrace();
			map.put("msg", "操作失败！");
		}
		
		return new ModelAndView(new MappingJackson2JsonView(), map);
	}

}
